package com.example.mobile2021606185zhn0417homework;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private TextView tvMainTitle;
    private Button btnMainSupplier;
    private Button btnMainBuy;
    private TextView tvMainResult;
    private Button btnMainClose;
    private Button btnMainLeave;
    private String[] parts = new String[]{"轿车引擎","轿车前玻璃","轿车方向盘"};
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        btnMainSupplier.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              String [] supplies = {"新星汽车配饰","枫叶汽车配饰","天辉汽车配饰","西末汽车配饰",};
              new AlertDialog.Builder(MainActivity.this)
                  .setTitle("请选择供应商")
                  .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                      @Override
                      public void onClick(DialogInterface dialog, int which) {
                          dialog.dismiss();
                      }
                  })
                      .setSingleChoiceItems(supplies, 0, new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialog, int which) {
                              tvMainResult.setText(supplies[which]);
                          }
                      })
                      .create()
                      .show();
            }
        });
        btnMainBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog;
                final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("请选择原材料");
                builder.setMultiChoiceItems(parts, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tvMainResult.setText(null);
                        for (int i = 0; i < parts.length-1; i++) {
                            if (listView.getCheckedItemPositions().get(i)) {
                                tvMainResult.append("已加购" +parts[i]+"\n"+"\n");
                            }
                        }
                        AlertDialog dialog1;
                        AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
                        builder.setTitle("加购成功");
                        builder.setMessage("订单已创建，请在规定时间内付款");
                        builder.setPositiveButton("好的",null);
                        builder.create();
                        builder.show();
                    }
                });
                dialog = builder.create();
                listView = dialog.getListView();
                dialog.show();
            }
        });
        btnMainLeave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(MainActivity.this)
                        .setTitle("官方提示信息")
                        .setMessage("确定要清空你的购物车吗")
                        .setNegativeButton("取消",null)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                tvMainResult.setText(null);
                                Toast.makeText(MainActivity.this,"购物车已清空",Toast.LENGTH_LONG).show();
                            }
                        })
                         .create()
                        .show();
            }
        });
        btnMainClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog dialog;
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("普通对话框");
                builder.setTitle("是否确定退出应用?");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                        MainActivity.this.finish();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                dialog = builder.create();
                dialog.show();
            }
        });
    }

    private void initView() {
        tvMainTitle = (TextView) findViewById(R.id.tv_main_title);
        btnMainSupplier = (Button) findViewById(R.id.btn_main_supplier);
        btnMainBuy = (Button) findViewById(R.id.btn_main_buy);
        tvMainResult = (TextView) findViewById(R.id.tv_main_result);
        btnMainClose = (Button) findViewById(R.id.btn_main_close);
        btnMainLeave = (Button) findViewById(R.id.btn_main_leave);
    }
}